import { EffectType, group } from "../../shared/interface";
import { template } from "../../shared/master/MsgAction";
import { baseCalss } from "../base/baseCalss";
import body_tool from "../fight/body/body_tool";
import { player, wallet_key } from "../fight/body/player";
import word from "./word";

export class wordStory extends baseCalss  {
    private _parent: word;
    storyMap: Map<string, any> = new Map();
    timeline: number = 0;
    private _time: any;
    constructor(table: Map<string, any> | undefined, parent: word) {
        super()
        this._parent = parent;
        if (!table) {
            console.error('缺少剧情故事表');
            return;
        }
        this.storyMap = table;
        this._time = setInterval(() => {
            this.tick()
        }, 60000)
    }
    destroy(){
        clearInterval(this._time);
        super.destroy();
    }
    tick() {
        this.timeline += 1;
        let story = this.storyMap.get(this.timeline.toString());
        if (!story) {
            // clearInterval(this._time);
            return;
        }
        //TODO:世界支持库
        /**
         * 
         * 刷新宝箱
         * 解锁全部怪物
         * 强制修改世界任务为完成状态
         * 修改地形 将地图修改为1格
         * 关闭地图显示
         */
        if (story.desc && story.desc.length > 0) {
            this._parent.wordCallMsg(template.default, `${story.title}\n${story.desc}`)
        }
        for (const key in story) {
            if (!key.includes('trigger')) {
                continue;
            }
            if (!story[key] || story[key].length == 0) {
                continue;
            }
            try {
                eval(`this.${story[key]}`)
            } catch (error) {
                debugger
            }
        }
    }
    /**
     * 设置世界全部格子 是否主动攻击玩家
     */
    tool_wordAllset_activeAttack(b:number){
        this._parent.cellGroup.forEach((e)=>{
            e.set_activeAttack(b);
        })
    }
    /**
     * 对所有玩家造成一次伤害
     * @param useName 
     * @param sk_name 
     * @param val 
     */
    tool_sendAllUser_to_tool_changeMp(useName:string,sk_name:string,val:number){
        this._parent.stayPlayer.forEach((e)=>{
            if(!(e instanceof player)){
                debugger;
                return;
            }
            e.add_hp({
                val: val,
                use_uuid: "1",
                use_name: useName,
                sk_name: sk_name,
                group: group.主场,
                type: EffectType.none
            },true)
        })
    }
    tool_activeAttackPlayer(){
        this._parent.cellGroup.forEach((e)=>{
            e.activeAttackPlayer();
        })
    }
    /**
     * 奖励全体玩家金币
     * @param num 
     */
    tool_awardAllUser(type: wallet_key, num: number) {
        let users = this._parent.stayPlayer;
        users.forEach(element => {
            element.wallet_change(type, num)
        });
    }
    /**
     * 解锁指定怪物
     */
    tool_unlock(id: string) {
        this._parent.tool_unlockEnemy(id);
    }
    /**
     * 修改所有格子pvp状态
     * @param b 
     */
    tool_PVP(b: boolean) {
        this._parent.tool_setPVP(b);
    }
    /**
     * 修改时间线
     * @param num 
     */
    too_changeTimeline(num: number) {
        this.timeline = num;
    }
}